package com.hertz.iris.view.alarm;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.actionbarsherlock.app.SherlockFragment;
import com.hertz.iris.R;
import com.hertz.iris.pojo.NormalAlarm;
import com.hertz.iris.util.BundleKeys;

public class TimeFragment extends SherlockFragment {
	
	@InjectView(R.id.hh)
	TextView hhText;
	@InjectView(R.id.mm)
	TextView mmText;
	
	private byte hh;
	private byte mm;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		super.onCreateView(inflater, container, savedInstanceState);
		View v = inflater.inflate(R.layout.time_fragment, container, false);
		ButterKnife.inject(this,v);
		Bundle ars = getArguments();
        NormalAlarm na = (NormalAlarm)ars.getSerializable(BundleKeys.NormalAlarmBundleKeys.NORMAL_ALARM_KEY);
		setHM(na.getHh(),na.getMm());
		return v;
	}
	
	@OnClick({R.id.hh,R.id.mm})
	void click(View view){
		popHHMMSelectorFragment();
	}
	
	public void setHM(byte hh,byte mm) {
		hhText.setText(String.valueOf(hh));
		mmText.setText(String.format("%02d", mm));
		Bundle ars = getArguments();
        NormalAlarm na = (NormalAlarm)ars.getSerializable(BundleKeys.NormalAlarmBundleKeys.NORMAL_ALARM_KEY);
        na.setHh(hh);
        na.setMm(mm);
	}

	private void popHHMMSelectorFragment(){
		HHMMSelectorFragment fragment = new HHMMSelectorFragment();
		fragment.setArguments(getArguments());
		FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
		transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.add(android.R.id.content, fragment,HHMMSelectorFragment.class.getSimpleName()).commit();
	}
	
}
